<template>
  <ex-dialog
    width="1200px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" :disabled="disabled" >
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品ID：" prop="merchantEquityCardId">
              <el-select v-model="ruleForm.merchantEquityCardId" placeholder="请选择商品" size="small" filterable>
                <el-option v-for="item in goodList" :key="item.id" :label="item.goodsId+' '+item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码：" prop="phone">
              <el-input
                maxlength="11"
                :formatter="formatInput"
                placeholder="请填写手机号码"
                v-model="ruleForm.phone">
              </el-input>
            </el-form-item> 
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员昵称：" prop="nickName">
              <el-input
                maxlength="50"
                placeholder="请填写会员昵称"
                v-model="ruleForm.nickName">
              </el-input>
            </el-form-item> 
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="评价内容：" prop="content"> 
              <el-input
                type="textarea"
                maxlength="200"
                :autosize="{ minRows: 5, maxRows: 10}"
                placeholder="请输入内容"
                v-model="ruleForm.content">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价图片:" ref="imgRef">
              <ImageUploadMore :limit="6" :fileSize="20" @successImg="getCommodityImgList($event)"
                    :value="ruleForm.imageArr" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="评分：" prop="score">
              <el-select v-model="ruleForm.score" placeholder="请选择评分" size="small" :disabled="pathType == 'edit'">
                <el-option v-for="item in scoreList" :key="item.id" :label="item.value" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item> 
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价时间：" prop="evaluateTime"> 
              <el-date-picker
                v-model="ruleForm.evaluateTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间"
               >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="button_box">
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm',2)"
      >保存提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
import Loading from "@/components/Loading/index";
// api
import {
  addEvaluate,
  editPopActivity,
  getSelectCardList,
  popActivityDetail,
} from "@/api/merchantsNFR/evaluate";
import '@wangeditor/editor/dist/css/style.css'
import {
  Editor,
  Toolbar
} from "@wangeditor/editor-for-vue";
import WangEditor from '@/components/WangEditor'
export default {
  components: {
    Loading,
    WangEditor,
    Editor,
    Toolbar
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
  
    var checkNickName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入"));
      } else if (value.length > 20 || value.length < 2) {
        return callback(new Error("请填写2~20位数"));
      } else {
        return callback();
      }
      
    };
    var checkNickPhone = (rule, value, callback) => {
      var regex = /[^0-9]/g;
      if (!value) {
        return callback(new Error("请输入手机号"));
      } else if (!regex.test(value) == false) {
        return callback(new Error("请输入正确的手机号码"));
      } else {
        return callback();
      }
      
    };
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "评价审核",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
          id: '',
          content: "", //评价内容
          evaluateTime: "", //评价时间
          merchantEquityCardId: "", //商品id
          imageArr: [], //图片数组
          nickName: '',//用户昵称
          phone: '',//手机号码
          score: 5,//总分
          evaluateTime: '',//	评价时间
      },
      residueStock: 0,
      goodList: [],//商品列表
      scoreList: [
        {id: 1, value: '一星'},
        {id: 2, value: '二星'},
        {id: 3, value: '三星'},
        {id: 4, value: '四星'},
        {id: 5, value: '五星'},
      ],//评分列表
      isSel: true,
      rules: {
        merchantEquityCardId: [
          { required: true,message: "请选择",trigger: ["change", "bulr"]}
        ],
        phone: [
          { required: true,validator: checkNickPhone,trigger: ["change", "bulr"]}
        ],
        nickName: [
          { required: true,validator: checkNickName,trigger: ["change", "bulr"]}
        ],
        content: [
          { required: true,message: "请输入内容",trigger: ["change", "bulr"]}
        ],
        score: [
          { required: true,message: "请选择评分",trigger: ["change", "bulr"]}
        ],
        evaluateTime: [
          {required: true,message: "请选择评价时间",trigger: ["change"]}
        ],
     

      },
      
    };
  },
  computed: {},
  created() {
    this.getSelectCardList()
    console.log('this.pathType',this.pathType)
    if(this.pathType == 'edit'){
      this.title = '编辑首页弹窗活动'
      this.ruleForm.id = this.id
      this.getInfo()
    }
   
  },
  mounted() {
   
    
  },
  computed: {},
  methods: {
    //获取详情
    getInfo(){
      var id = this.ruleForm.id
      popActivityDetail(id).then(res => {
        this.ruleForm = {
          ...res.result,
          date: [],
        }
        this.ruleForm.cardId = []
        console.log('this.ruleForm',this.ruleForm)
   
        this.ruleForm.date[0] = res.result.startTime
        this.ruleForm.date[1] = res.result.endTime
      })
    },
   
    //获取商品下拉
    getSelectCardList(){
      getSelectCardList().then( res => {
        this.goodList = res.result
      })
    },
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    getCommodityImgList(e, index) {
      console.log(e, index)
      if (e != '') {
        this.ruleForm.imageArr = e.split(",")
      } else {
        this.ruleForm.imageArr = []
      }
      this.$refs['commodityImgListRef'].clearValidate();
    },
    formatInput (value) {
    return value.replace(/[^\d.,]/g, '');
    },
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.AgreeLoading = true
          var data = {
            ...this.ruleForm
          }
          if(this.pathType == 'add'){
            addEvaluate(data).then(res => {
              this.AgreeLoading = false
              this.handleClose()
              this.$message.success('创建成功')
              this.$emit('success')
              
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }else if(this.pathType == 'edit'){
            editPopActivity(data).then(res => {
              this.handleClose()
                this.$message.success('修改成功')
                this.$emit('success')
                this.AgreeLoading = false
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    editForm(formName, status) {
      //编辑确定
      console.log(this.ruleForm);
      this.$refs[formName].validate(valid => {
        if (valid) {
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}
.el-select{
  width: 100%;
}
.main {
  padding: 20px 20px;
}
.richText {
  border: 1px solid #ccc;
  line-height: normal;
}
.stockNum{
  display: flex;
  justify-content: center;
  padding-top: 10px;
}
.button_box{
  display: flex;
  justify-content: center;
}
.equityImgBox{
  width: 100%;
  display: flex;
  justify-content: center;
}
.equityImgBox img{
  width: 120px;
  height: 120px;
}
.el-form-item{
  margin-bottom: 32px;
}
.list_start{
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .el-input{
    width: 300px;
    margin-left: 10px;
  }
}
</style>
